package com.jprocms.module.cms.api.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mzt.logapi.starter.annotation.DiffLogField;
import lombok.Data;

import java.io.Serializable;
import java.util.Arrays;
import java.util.StringJoiner;

@Data
public class UploadConfig implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 允许上传的图片类型。格式如：jpg,jpeg,jfif,pjpeg,pjp,png,gif,webp
     */
    @DiffLogField(name = "允许上传的图片类型")
    private String imageLimits = "jpg,jpeg,jfif,pjpeg,pjp,png,gif,webp";
    /**
     * 允许上传的视频类型。格式如：mp4,webm,ogg
     */
    @DiffLogField(name = "允许上传的视频类型")
    private String videoLimits = "mp4,webm,ogg";
    /**
     * 允许上传的音频格式。格式如：mp3,ogg,wav
     */
    @DiffLogField(name = "允许上传的音频格式")
    private String audioLimits = "mp3,ogg,wav";
    /**
     * 允许上传的文库类型。格式如：doc,docx,xls,xlsx,ppt,pptx,pdf
     */
    @DiffLogField(name = "允许上传的文库类型")
    private String libraryLimits = "doc,docx,xls,xlsx,ppt,pptx,pdf";
    /**
     * 允许上传的附件类型。格式如：zip,7z,gz,bz2,iso,rar,doc,docx,xls,xlsx,ppt,pptx,pdf,mp3,ogg,wav,mp4,webm,ogg,jpg,jpeg,jfif,pjpeg,pjp,png,gif,webp
     */
    @DiffLogField(name = "允许上传的附件类型")
    private String attachLimits = "zip,7z,gz,bz2,iso,rar,doc,docx,xls,xlsx,ppt,pptx,pdf,mp3,ogg,wav,mp4,webm,ogg,jpg,jpeg,jfif,pjpeg,pjp,png,gif,webp";
    /**
     * 图片最大。单位 MB
     */
    @DiffLogField(name = "图片最大")
    private int imageLimit = 0;
    /**
     * 视频最大。单位 MB
     */
    @DiffLogField(name = "视频最大")
    private int videoLimit = 0;
    /**
     * 音频最大。单位 MB
     */
    @DiffLogField(name = "音频最大")
    private int audioLimit = 0;
    /**
     * 文库最大。单位 MB
     */
    @DiffLogField(name = "文库最大")
    private int libraryLimit = 0;
    /**
     * 附件最大。单位 MB
     */
    @DiffLogField(name = "附件最大")
    private int attachLimit = 0;

    @JsonIgnore
    public long getImageLimitByte() {
        return this.imageLimit * 1024L * 1024L;
    }

    @JsonIgnore
    public String getImageLimitByteStr() {
        return new Long(this.imageLimit * 1024L * 1024L).toString();
    }

    @JsonIgnore
    public String getImageInputAccept() {
        return toInputAccept(imageLimits);
    }

    @JsonIgnore
    public String getAudioInputAccept() {
        return toInputAccept(audioLimits);
    }

    @JsonIgnore
    public String getVideoInputAccept() {
        return toInputAccept(videoLimits);
    }

    @JsonIgnore
    public String getLibraryInputAccept() {
        return toInputAccept(libraryLimits);
    }

    @JsonIgnore
    public String getAttachInputAccept() {
        return toInputAccept(attachLimits);
    }

    /**
     * 标准的允许上传后缀以逗号分割后缀名，如 `jpg,jpeg,png,gif`，而 input 的 accept 格式为 .jpg,.jpeg,.png,.gif。
     *
     * @param types `jpg,jpeg,png,gif`格式的后缀名
     * @return `.jpg,.jpeg,.png,.gif` 格式的后缀名
     */
    public static String toInputAccept(String types) {
        StringJoiner joiner = new StringJoiner(",.", ".", "");
        Arrays.asList(types.split(",")).forEach(joiner::add);
        return joiner.toString();
    }

    @JsonIgnore
    public long getVideoLimitByte() {
        return this.videoLimit * 1024L * 1024L;
    }

    @JsonIgnore
    public long getAudioLimitByte() {
        return this.audioLimit * 1024L * 1024L;
    }

    @JsonIgnore
    public long getLibraryLimitByte() {
        return this.libraryLimit * 1024L * 1024L;
    }

    @JsonIgnore
    public long getAttachLimitByte() {
        return this.attachLimit * 1024L * 1024L;
    }

}

